﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Common;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Models.Blog;
using X.PagedList;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    public class BlogController : Controller
    {
        private IBlogService _ibgSvc;
        private IReleaseFormService _irfSvc;
        private IArticleCateGoryService _iacSvc;
        private ILevelService _levelSvc;
        private IUsersService _usersSvc;
        public BlogController(IBlogService ibgSvc, 
                              IReleaseFormService irfSvc, 
                              IArticleCateGoryService iacSvc,
                              ILevelService levelSvc,
                              IUsersService usersSvc)
        {
            _ibgSvc = ibgSvc;
            _irfSvc = irfSvc;
            _iacSvc = iacSvc;
            _levelSvc = levelSvc;
            _usersSvc = usersSvc;
        }

        [HttpGet]
        public async Task<IActionResult> Index(string search, int page = 1,int stateCode = 1)
        {
            search = string.IsNullOrEmpty(search) ? "" : search;
            var data = await _ibgSvc.GetByNameAsync(search,1,stateCode);
            var BlogList = new List<BlogListViewModel>();
            foreach (var item in data) 
            {
                var model = new BlogListViewModel
                {
                    Id = item.Id,
                    Title = item.Title,
                    Content = item.Content,
                    PublishTime = item.PublishTime,
                    StateCode = item.StateCode,
                    Like = item.Like,
                    Trample = item.Trample,
                    UpdateTime = item.UpdateTime,
                    ReleaseFormTitle = await GetReleaseFormTitle(item.FbxsId),
                    ArticleCateTitle = await GetArticleCateTitle(item.TypeId),
                    LevelTitle = await GetLevelTitle(item.LevelId),
                    UsersTitle = await GetUsersTitle(item.Uid)
                };
                BlogList.Add(model);
            }
            ViewBag.Search = search;
            ViewBag.Page = page;
            var pages = await BlogList.ToPagedListAsync(page,PageConfig.PageSize);
            return View(pages);
        }

        private async Task<string> GetReleaseFormTitle(int id) 
        {
            var data = await _irfSvc.GetByIdAsync(id);
            return data == null ? "" : data.Title;
        }

        private async Task<string> GetArticleCateTitle(int id) 
        {
            var data = await _iacSvc.GetByIdAsync(id);
            return data == null ? "" : data.Title;
        }

        private async Task<string> GetLevelTitle(int id) 
        {
            var data = await _levelSvc.GetByIdAsync(id);
            return data == null ? "" : data.Title;
        }

        private async Task<string> GetUsersTitle(int id) 
        {
            var data = await _usersSvc.GetByIdAsync(id);
            return data == null ? "" : data.NickName;
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id) 
        {
            if (ModelState.IsValid) 
            {
                await _ibgSvc.PutTrashAsync(id);
                return Redirect("~/ForumManage/Blog/Index");
            }
            return View();
        }

        [HttpGet]
        public async Task<IActionResult> UpdateStateCode(int page, int stateCode) 
        {
            if (ModelState.IsValid) 
            {
                var res = await _ibgSvc.UpdateStateCode(page, stateCode);
                if (res > 0) 
                {
                    return Redirect("~/ForumManage/Blog/Index");
                }
            }
            return Redirect("~/ForumManage/Blog/Index");
        }

    }
}
